package com.gxa.controller.system;

import com.gxa.annotation.Perm;
import com.gxa.pojo.entity.system.Role;
import com.gxa.pojo.entity.system.User;
import com.gxa.service.system.RoleService;
import com.gxa.service.system.UserService;
import com.gxa.utils.MessageCode;
import com.gxa.utils.QueryResult;
import com.gxa.utils.Result;
import com.gxa.utils.ResultUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @Author lch
 * @Date 2024/2/22 16:04
 * @Description
 **/
@RestController
public class RoleController {

    @Resource
    private RoleService roleService;

    /**
     * 添加角色
     * @param role
     * @return
     */
    @Perm("role:add")
    @PostMapping("/role/add")
    public Result addRole(Role role) {
        int result = roleService.addRole(role);
        if (result > 0) {
            return ResultUtils.buildSuccess(MessageCode.REQUEST_SUCCESS);
        }
        return ResultUtils.buildFail(MessageCode.SYSTEM_ERROR);
    }

    /**
     * 修改角色
     * @param role
     * @return
     */
    @Perm("role:update")
    @PostMapping("/role/update")
    public Result updateUser(Role role) {
        int result = roleService.updateRole(role);
        if (result > 0) {
            return ResultUtils.buildSuccess(MessageCode.REQUEST_SUCCESS);
        }
        return ResultUtils.buildFail(MessageCode.SYSTEM_ERROR);
    }


    /**
     * 删除角色
     * @param id 角色id
     * @return
     */
    @Perm("role:delete")
    @PostMapping("/role/delete")
    public Result deleteRole(Integer id) {
        int result = roleService.deleteRoleById(id);
        if (result > 0) {
            return ResultUtils.buildSuccess(MessageCode.REQUEST_SUCCESS);
        }
        return ResultUtils.buildFail(MessageCode.SYSTEM_ERROR);
    }

    /**
     * 查询角色
     * @param role
     * @return
     */
    @Perm("role:query")
    @GetMapping("/role/query")
    public Result queryUser(Role role) {
        QueryResult result = roleService.queryAllRole(role);
        return ResultUtils.buildSuccess(result.getData(), result.getCount());
    }
}
